﻿using System;
using System.Data;
using System.Web;
using Arizu.Repository;

namespace AiRiZu.User.handler
{
    public class GetSubTotal : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int price = 0;

            long roomId = long.Parse(context.Request.Params["RoomId"].ToString());
            string startDate = context.Request.Params["startDate"].ToString();
            string endDate = context.Request.Params["endDate"].ToString();

            int NumOfNight = 0;

            AirizuUser userDao = AirizuUser.CreateInstance();

            string CheckInDate = context.Request.Params["startDate"] == null ? "" : context.Request.Params["startDate"].ToString();
            string CheckOutDate = context.Request.Params["endDate"] == null ? "" : context.Request.Params["endDate"].ToString();

            DateTime checkinDateTime = DateTime.ParseExact(CheckInDate, "yyyy-MM-dd", null);
            DateTime checkoutDateTime = DateTime.ParseExact(CheckOutDate, "yyyy-MM-dd", null);

            TimeSpan checkinTimeSpan = new TimeSpan(checkinDateTime.Ticks);
            TimeSpan checkoutTimeSpan = new TimeSpan(checkoutDateTime.Ticks);

            TimeSpan span = checkoutTimeSpan.Subtract(checkinTimeSpan);

            if (span.TotalDays < 1)
            {
                NumOfNight = 1;
            }
            else
	        {       
                NumOfNight = Convert.ToInt32(span.TotalDays);
	        }

            AirizuRoom airizuRoom = AirizuRoom.CreateInstance();
            DataTable dt1 = airizuRoom.GetRoom(roomId);
            if (dt1.Rows.Count > 0)
            {
                long ownerId = long.Parse(dt1.Rows[0]["OwnerID"].ToString());

                AirizuPrice airizuPrice = AirizuPrice.CreateInstance();
                DataTable dt = airizuPrice.GetPrice(roomId);

                if (NumOfNight > 30 && dt.Rows[0]["MonthPrice"].ToString() != null)
                {
                    price = GetPrice(int.Parse(dt.Rows[0]["MonthPrice"].ToString()), NumOfNight);
                }
                else if ((NumOfNight < 30 && NumOfNight > 7) && dt.Rows[0]["WeekPrice"].ToString() != null)
                {
                    price = GetPrice(int.Parse(dt.Rows[0]["WeekPrice"].ToString()), NumOfNight);
                }
                else if (NumOfNight < 7 && dt.Rows[0]["DayPrice"].ToString() != null)
                {
                    price = GetPrice(int.Parse(dt.Rows[0]["DayPrice"].ToString()), NumOfNight);
                }
            }

            int count = 0;
            AirizuIndent airizuIndent = AirizuIndent.CreateInstance();
            DataTable dtIndent = airizuIndent.IsAvailable(roomId, checkinDateTime.Ticks, checkoutDateTime.Ticks);
            if (dtIndent.Rows.Count > 0)
            {
                count = int.Parse(dtIndent.Rows[0][0].ToString());
            }

            bool isAvailable = false;
            if (count > 0)
            {
                isAvailable = false;
            }
            else 
            {
                isAvailable = true;
            }
            string msg = "{\"subTotal\":\"" + price.ToString() + "\",\"available\":\"" + isAvailable + "\"}"; 
            context.Response.Write(msg);
        }

        private int GetPrice(int price, int num)
        {
            return price * num;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
